package com.cyh.exp.service.Impl;

import com.cyh.exp.mapper.TaskMapper;
import com.cyh.exp.mode.*;
import com.cyh.exp.service.TaskService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {

    private TaskMapper taskMapper;

    private TaskServiceImpl(TaskMapper taskMapper){this.taskMapper=taskMapper;}

    @Override
    public Result<List> getAllTask(String username) {
        List<Task_list> task_lists = taskMapper.getAllTask(username);
        System.out.println(task_lists);
        System.out.println(username);
        if (task_lists == null){
            return new Result<List>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getType(), "查询成功，暂无任务",null);
        }
        else {
            return new Result<List>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getType(), "查询成功",task_lists);
        }
    }

    @Override
    public Result InsertTask(Task_release taskRelease) {
        if (taskRelease.getJsuser().indexOf(",")!=0){
            String[] arruser = taskRelease.getJsuser().split(",");
            System.out.println(arruser);
            for (int i=0;i< arruser.length;i++){
                Task_user task_user = new Task_user();
                task_user.setTid(taskRelease.getId());
                task_user.setJrid(arruser[i]);
                taskMapper.InsertUser(task_user);
            }
        }else {
            Task_user task_user = new Task_user();
            task_user.setTid(taskRelease.getId());
            task_user.setJrid(taskRelease.getJsuser());
            taskMapper.InsertUser(task_user);
        }
        int result = taskMapper.InsertTask(taskRelease);
        if (result == 1){
            return new Result<AffectedRows>(
                    ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getType(), "发布成功",new AffectedRows(result)
            );
        }
        else {
            return new Result<AffectedRows>(
                    ReturnCode.ERROR.getCode(), ReturnCode.ERROR.getType(), "发布失败",new AffectedRows(result)
            );
        }
    }

    @Override
    public Result SubTask(Task_sub task_sub) {
        Task_status task_status = new Task_status();
        task_status.setJrid(task_sub.getJrid());
        task_status.setTid(task_sub.getTid());
        task_status.setStatus(1);
        int result1 = taskMapper.Examine(task_status);
        int result = taskMapper.InsertTaskCont(task_sub);
        if (result==1 && result1==1){
            return new Result<AffectedRows>(
                    ReturnCode.SUCCESS.getCode(),ReturnCode.SUCCESS.getType(), "提交成功",new AffectedRows(result)
            );
        }
        else {
            return new Result<AffectedRows>(
                    ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getType(), "提交失败",new AffectedRows(result)
            );
        }
    }

    @Override
    public Result Examine(Task_status task_status) {
        int result = taskMapper.Examine(task_status);
        if (result==1){
            return new Result<AffectedRows>(
                    ReturnCode.SUCCESS.getCode(),ReturnCode.SUCCESS.getType(), "提交成功",new AffectedRows(result)
            );
        }
        else {
            return new Result<AffectedRows>(
                    ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getType(), "提交失败",new AffectedRows(result)
            );
        }
    }

    @Override
    public Result getSubCont(String tid, String jrid) {
        Task_sub task_sub = taskMapper.getSubCont(tid, jrid);
        if (task_sub!=null){
            return new Result<Task_sub>(
                    ReturnCode.SUCCESS.getCode(),
                    ReturnCode.SUCCESS.getType(),
                    "查询成功",task_sub
            );
        }
        else
        {
            return new Result<String>(
                    ReturnCode.ERROR.getCode(),
                    ReturnCode.ERROR.getType(),
                    "查询失败",null
            );
        }
    }

    @Override
    public Result getCont(String tid) {
        Task_cont task_cont = taskMapper.getContnt(tid);
        if (task_cont!=null){
            return new Result<Task_cont>(
                    ReturnCode.SUCCESS.getCode(),
                    ReturnCode.SUCCESS.getType(),
                    "查询成功",task_cont
            );
        }
        else
        {
            return new Result<String>(
                    ReturnCode.ERROR.getCode(),
                    ReturnCode.ERROR.getType(),
                    "查询失败",null
            );
        }
    }


}
